﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.Koc;
using System.Collections.ObjectModel;
using System.Collections;

namespace CSharp.Libs.WPF.UserControls
{
    public class KocAllianceMemberListViewModel
    {
        #region declarations
        private Kernel _KRNL = null;

        private ObservableCollection<KocAllianceMembersListItem> _ocAllianceMembers = new ObservableCollection<KocAllianceMembersListItem>();

        private int _intAllianceId = -1;
        #endregion
        


        #region constructor / finalizer
        public KocAllianceMemberListViewModel(
            Kernel KRNL,
            int AllianceId)
        {
            _KRNL = KRNL;
            _intAllianceId = AllianceId;
        }
        #endregion
        
        
        
        #region event handling
        #endregion
        
        
        
        #region private functions
        #region member list
        /// <summary>_SetAllianceMemberUrls
        /// 
        /// </summary>
        /// <param name="AllianceId"></param>
        private void _SetAllianceMemberUrls()
        {
            try
            {
                if (_KRNL.UH.URLAllianceGetOtherInfo.URLMaybeValid)
                {
                    if (!_KRNL.UH.URLGetUserLeaderboard.URLMaybeValid)
                    {
                        _KRNL.UH.ParseURLGetUserLeaderboard(_KRNL.UH.URLAllianceGetOtherInfo.GetURL().Replace(URLHolder.PHP_ALLIANCE_GET_OTHER_INFO, URLHolder.PHP_GET_USER_LEADERBOARD));
                        _KRNL.UH.URLGetUserLeaderboard.Parameters.Remove(URLHolder.PARAM_PAGE_NO);
                        _KRNL.UH.URLGetUserLeaderboard.Parameters.Remove(URLHolder.PARAM_CITY_ID);
                        _KRNL.UH.URLGetUserLeaderboard.Parameters.Add(URLHolder.PARAM_ALLIANCE_ID, "");
                        _KRNL.UH.URLGetUserLeaderboard.Parameters.Add(URLHolder.PARAM_PER_PAGE, 100.ToString());
                    }

                    if (!_KRNL.UH.URLViewCourt.URLMaybeValid)
                    {
                        _KRNL.UH.ParseURLViewCourt(_KRNL.UH.URLAllianceGetOtherInfo.GetURL().Replace(URLHolder.PHP_ALLIANCE_GET_OTHER_INFO, URLHolder.PHP_VIEW_COURT));
                        _KRNL.UH.URLViewCourt.Parameters.Remove(URLHolder.PARAM_PAGE_NO);
                        _KRNL.UH.URLViewCourt.Parameters.Remove(URLHolder.PARAM_CITY_ID);
                        _KRNL.UH.URLViewCourt.Parameters.Add(URLHolder.PARAM_PID, "");
                    }

                    if (!_KRNL.UH.URLGetOnline.URLMaybeValid)
                    {
                        _KRNL.UH.ParseURLGetOnline(_KRNL.UH.URLAllianceGetOtherInfo.GetURL().Replace(URLHolder.PHP_ALLIANCE_GET_OTHER_INFO, URLHolder.PHP_GET_ONLINE));
                        _KRNL.UH.URLGetOnline.Parameters.Remove(URLHolder.PARAM_PAGE_NO);
                        _KRNL.UH.URLGetOnline.Parameters.Remove(URLHolder.PARAM_CITY_ID);
                        _KRNL.UH.URLGetOnline.Parameters.Add(URLHolder.PARAM_CHECK_ARR, "");
                    }
                }
                else
                {
                    _KRNL.EC.AddException(new ArgumentException("Parent URL is not valid."), this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_GetMemberList
        /// 
        /// </summary>
        private void _GetMemberList(
            String AllianceId)
        {
            try
            {
                _SetAllianceMemberUrls();

                if (_KRNL.UH.URLGetUserLeaderboard.URLMaybeValid)
                {
                    _ocAllianceMembers.Clear();

                    _KRNL.UH.URLGetUserLeaderboard.Parameters[URLHolder.PARAM_ALLIANCE_ID] = AllianceId;

                    Hashtable htResult = null;
                    if (_KRNL.UH.URLGetUserLeaderboard.GetAnswerHashtable(out htResult))
                    {
                        Boolean fResultOk = false;
                        Boolean.TryParse(htResult[constants.KEY_OK].ToString(), out fResultOk);
                        if (fResultOk)
                        {
                            ArrayList _aryResults = htResult[constants.KEY_RESULTS] as ArrayList;
                            if (_aryResults != null)
                            {
                                String strCheckArr = "";
                                Boolean fFirst = true;

                                foreach (Hashtable HT in _aryResults)
                                {
                                    try
                                    {
                                        KocAllianceMembersListItem KAMLI = new KocAllianceMembersListItem();

                                        ArrayList aryCities = HT[constants.KEY_CITIES] as ArrayList;
                                        if (aryCities != null)
                                        {
                                            foreach (Hashtable htCity in aryCities)
                                            {
                                                KocAllianceMembersCityItem KAMCI = new KocAllianceMembersCityItem();

                                                KAMCI.CityId = htCity[constants.KEY_CITY_ID].ToString();
                                                KAMCI.CityName = htCity[constants.KEY_CITY_NAME].ToString();
                                                KAMCI.XCoord = htCity[constants.KEY_CITY_X_COORD].ToString();
                                                KAMCI.YCoord = htCity[constants.KEY_CITY_Y_COORD].ToString();

                                                KAMLI.Cities.Add(KAMCI);
                                            }
                                        }

                                        KAMLI.AllianceId = htResult[constants.KEY_ALLIANCE_ID].ToString();
                                        KAMLI.AllianceName = htResult[constants.KEY_ALLIANCE_NAME].ToString();

                                        if (_strSelectedAlliance != KAMLI.AllianceName)
                                        {
                                            _strSelectedAlliance = KAMLI.AllianceName;
                                            OnPropertyChanged("SelectedAlliance");
                                        }

                                        KAMLI.Rank = HT[constants.KEY_RANK].ToString();
                                        KAMLI.UserId = HT[constants.KEY_USER_ID].ToString();
                                        KAMLI.DisplayName = HT[constants.KEY_DISPLAY_NAME].ToString();
                                        KAMLI.PlayerSex = HT[constants.KEY_PLAYER_SEX].ToString();
                                        KAMLI.AvatarId = HT[constants.KEY_AVATAR_ID].ToString();
                                        KAMLI.WarStatus = HT[constants.KEY_WAR_STATUS].ToString();
                                        try
                                        {
                                            KAMLI.Might = Int32.Parse(HT[constants.KEY_MIGHT].ToString()).ToString("0,000");
                                        }
                                        catch
                                        {
                                        }
                                        KAMLI.Title = HT[constants.KEY_TITLE].ToString();
                                        //KAMLI.OfficerType = HT[KEY_OFFICER_TYPE].ToString();
                                        switch (HT[constants.KEY_OFFICER_TYPE].ToString())
                                        {
                                            case "1":
                                                KAMLI.OfficerType = "Chancelor";
                                                break;
                                            case "2":
                                                KAMLI.OfficerType = "Vice Chancelor";
                                                break;
                                            case "3":
                                                KAMLI.OfficerType = "Officer";
                                                break;
                                            default:
                                                KAMLI.OfficerType = "";
                                                break;
                                        }
                                        KAMLI.NumCities = HT[constants.KEY_NUM_CITIES].ToString();

                                        //Build CheckArr parameter
                                        if (fFirst)
                                        {
                                            strCheckArr = KAMLI.UserId;
                                            fFirst = false;
                                        }
                                        else
                                        {
                                            strCheckArr = String.Concat(strCheckArr, ",", KAMLI.UserId);
                                        }

                                        if (_fExecuteViewCourt)
                                        {
                                            //Get ViewCourt info
                                            Hashtable htViewCourt = _GetViewCourt(Int32.Parse(KAMLI.UserId));
                                            if (htViewCourt != null)
                                            {
                                                Hashtable htPlayerInfo = htViewCourt[constants.KEY_PLAYER_INFO] as Hashtable;
                                                if (htPlayerInfo != null)
                                                {
                                                    KAMLI.DailyActionFlag = htViewCourt[constants.KEY_DAILY_ACTION_FLAG].ToString();

                                                    KAMLI.Email = htPlayerInfo[constants.KEY_EMAIL].ToString();
                                                    KAMLI.FBUID = htPlayerInfo[constants.KEY_FB_UID].ToString();
                                                    KAMLI.UserType = htPlayerInfo[constants.KEY_USER_TYPE].ToString();
                                                    KAMLI.Status = htPlayerInfo[constants.KEY_STATUS].ToString();
                                                    KAMLI.DateJoined = htPlayerInfo[constants.KEY_DATE_JOINED].ToString();
                                                    KAMLI.LastLogin = htPlayerInfo[constants.KEY_LAST_LOGIN].ToString();
                                                    KAMLI.EventTimeStamp = htPlayerInfo[constants.KEY_EVENT_TIMESTAMP].ToString();
                                                    KAMLI.EventStatus = htPlayerInfo[constants.KEY_EVENT_STATUS].ToString();
                                                    KAMLI.TruceExpireTimestamp = htPlayerInfo[constants.KEY_TRUCE_EXPIRE_TIMESTAMP].ToString();
                                                    KAMLI.FogExpireTimestamp = htPlayerInfo[constants.KEY_FOG_EXPIRE_TIMESTAMP].ToString();
                                                    KAMLI.CntNewMsg = htPlayerInfo[constants.KEY_CNT_NEWMSG].ToString();
                                                    KAMLI.CntFriendReq = htPlayerInfo[constants.KEY_CNT_FRIENDREQ].ToString();
                                                    KAMLI.CntLogins = htPlayerInfo[constants.KEY_CNT_LOGINS].ToString();
                                                    KAMLI.CntPassReset = htPlayerInfo[constants.KEY_CNT_PASSRESET].ToString();
                                                    KAMLI.CntConnections = htPlayerInfo[constants.KEY_CNT_CONNECTIONS].ToString();
                                                    //KAMLI.PhotoHost = htPlayerInfo[KEY_PHOTO_HOST].ToString();
                                                    //KAMLI.PhotoDir = htPlayerInfo[KEY_PHOTO_DIR].ToString();
                                                    //KAMLI.PhotoSubDir = htPlayerInfo[KEY_PHOTO_SUBDIR].ToString();
                                                    //KAMLI.PhotoName = htPlayerInfo[KEY_PHOTO_NAME].ToString();
                                                }

                                            }
                                        }

                                        _ocAllianceMembers.Add(KAMLI);
                                    }
                                    catch (Exception ex)
                                    {
                                        _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
                                    }
                                }

                                //Get Online info
                                if (strCheckArr != "")
                                {
                                    _KRNL.UH.URLGetOnline.Parameters[URLHolder.PARAM_CHECK_ARR] = strCheckArr;

                                    Hashtable htOnline = null;
                                    if (_KRNL.UH.URLGetOnline.GetAnswerHashtable(out htOnline))
                                    {
                                        fResultOk = false;
                                        Boolean.TryParse(htOnline[constants.KEY_OK].ToString(), out fResultOk);
                                        if (fResultOk)
                                        {
                                            Hashtable htData = htOnline[constants.KEY_DATA] as Hashtable;
                                            if (htData != null)
                                            {
                                                foreach (KocAllianceMembersListItem KAMLI in _ocAllianceMembers)
                                                {
                                                    if (Boolean.Parse(htData[KAMLI.UserId].ToString()))
                                                    {
                                                        KAMLI.Online = "ONLINE";
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //_strResult = "Server didn't return ArrayList.";
                            }
                        }
                        else
                        {
                            //_strResult = "Server returned false.";
                        }
                    }
                    else
                    {
                        //_strResult = "Execute didn't return true.";
                    }
                }
                else
                {
                    //_strResult = "URL is not valid.";
                }

                OnPropertyChanged("AllianceMembers");
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_GetViewCourt
        /// 
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        private Hashtable _GetViewCourt(
            Int32 UserId)
        {
            try
            {
                if (UserId > 0)
                {
                    _KRNL.UH.URLViewCourt.Parameters[URLHolder.PARAM_PID] = UserId.ToString();

                    Hashtable HT = null;
                    _KRNL.UH.URLViewCourt.GetAnswerHashtable(out HT);

                    if (HT != null)
                    {
                        return HT;
                    }
                }
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
            }

            return null;
        }
        #endregion
        #endregion
        
        
        
        #region interface
        #region properties
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
